﻿
using Newtonsoft.Json;
using System;

namespace TravisLight.Model.Entity
{
    public class Repo
    {
        #region properties

        public int Id
        {
            get;
            set;
        }

        public string Slug
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        [JsonProperty("last_build_result")]
        public Nullable<bool> LastBuildResult
        {
            get;
            set;
        }

        [JsonProperty("last_build_started_at")]
        public Nullable<DateTime> LastBuildStartedAt
        {
            get;
            set;
        }

        [JsonProperty("last_build_finished_at")]
        public Nullable<DateTime> LastBuildFinishedAt
        {
            get;
            set;
        }

        public string Status
        {
            get
            {
                if (LastBuildStartedAt.HasValue && !LastBuildFinishedAt.HasValue)
                {
                    return TravisLight.Model.Status.Building;
                }

                if (LastBuildResult.HasValue)
                {
                    return LastBuildResult.Value ? TravisLight.Model.Status.Failed : TravisLight.Model.Status.Passed;
                }

                return TravisLight.Model.Status.Unknown;
            }
        }

        public int Rank
        {
            get
            {
                switch (Status)
                {
                    case TravisLight.Model.Status.Passed:
                        return 4;

                    case TravisLight.Model.Status.Failed:
                        return 2;

                    case TravisLight.Model.Status.Building:
                        return 1;
                }

                return 3;
            }
        }

        #endregion
    }
}
